﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class cardcontrol : MonoBehaviour
{
    GameObject deckbuilderPanel;//iventorypanel
    public GameObject maindeckSlot;//iventoryslot
    public GameObject cardItem;//iventoryitem
    carddatabase database;

    public int slotAmount;
    public List<Card> cards = new List<Card>();
    public List<GameObject> slots = new List<GameObject>();

    void Start()
    {
        database = GetComponent<carddatabase>();

        slotAmount = 32;
        deckbuilderPanel = GameObject.Find("deckbuilderpanel");
        for (int i = 0; i < slotAmount; i++)
        {
            cards.Add(new Card());
            slots.Add(Instantiate(maindeckSlot));
            slots[i].GetComponent<slot>().id = i;
            slots[i].transform.SetParent(deckbuilderPanel.transform);
        }
    }
    bool CheckIfcardisindeckbuild(Card card)
    {
        for (int i = 0; i < cards.Count; i++)
        {
            if (cards[i].Id == card.Id)
                return true;
        }
        return false;
    }

    public void AddCard(string name)
    {
        Card cardtoAdd = database.fetchcardbyname(name);
        if (cardtoAdd.Stackable && CheckIfcardisindeckbuildbyname(cardtoAdd))
        {
            for (int i = 0; i < cards.Count; i++)
            {
                if (string.Equals(cards[i].Name, name))
                {
                    carddata data = slots[i].transform.GetChild(0).GetComponent<carddata>();
                    if (data.amount >= 4) return;
                    data.amount++;
                    data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
                    break;
                }
            }
        }
        else
        {
            for (int i = 0; i < cards.Count; i++)
            {
                if (string.IsNullOrEmpty(cards[i].Name))
                {
                    cards[i] = cardtoAdd;
                    GameObject cardobj = Instantiate(cardItem);
                    cardobj.GetComponent<carddata>().card = cardtoAdd;
                    cardobj.GetComponent<carddata>().slot = i;
                    cardobj.GetComponent<carddata>().amount = 1;
                    cardobj.transform.SetParent(slots[i].transform);
                    cardobj.transform.position = slots[i].transform.position;
                    cardobj.GetComponent<Image>().sprite = cardtoAdd.Sprite;
                    cardobj.name = cardtoAdd.Name;
                    cardobj.transform.GetChild(0).GetComponent<Text>().text = "1";
                    break;
                }
            }
        }
    }
    bool CheckIfcardisindeckbuildbyname(Card card)
    {
        for (int i = 0; i < cards.Count; i++)
        {
            if (cards[i].Name == card.Name)
                return true;
        }
        return false;
    }

    public void Removecard(string name)
    {
        Card cardToRemove = database.fetchcardbyname(name);
        if (cardToRemove.Stackable && CheckIfcardisindeckbuildbyname(cardToRemove))
        {
            for (int j = 0; j < cards.Count; j++)
            {
                if (cards[j].Name == name)
                {
                    carddata data = slots[j].transform.GetChild(0).GetComponent<carddata>();
                    data.amount--;
                    data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
                    if (data.amount == 0)
                    {
                        Destroy(slots[j].transform.GetChild(0).gameObject); cards[j] = new Card();
                        break;
                    }
                    if (data.amount == 1)
                    {
                        slots[j].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = "1";
                        break;
                    }
                    break;
                }
            }
        }
    }
}
